package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.es.domain.DestinationEs;
import cn.wolfcode.luowowo.es.domain.StrategyEs;
import cn.wolfcode.luowowo.es.domain.TravelEs;
import cn.wolfcode.luowowo.es.domain.UserInfoEs;
import cn.wolfcode.luowowo.es.service.ISearchService;
import cn.wolfcode.luowowo.query.SearchQueryObject;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//@RequestMapping("q")
public class SearchController {
    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ISearchService searchService;

//    @GetMapping("/q")
//    public Object searchAll(@ModelAttribute("qo")SearchQueryObject qo) throws UnsupportedEncodingException {
//        String keyword = URLDecoder.decode(qo.getKeyword(), "utf-8");
//        qo.setKeyword(keyword);
//        Map<String, Object> map = new HashMap<>();
//        Map<String, Object> result = new HashMap<>();
//        List<Strategy> strategies = strategyService.listByKeyword(qo);
//        List<Travel> travels = travelService.listByKeyword(qo);
//        List<UserInfo> users = userInfoService.listByKeyword(qo);
//        if (qo.getType() == 0) {
//            Destination destination = destinationService.findByKeyword(qo);
//            result.put("strategies", strategies);
//            result.put("travels", travels);
//            result.put("users", users);
//            if (destination != null) {
//                map.put("dest", destination);
//                result.put("total", 1 + strategies.size()
//                        + travels.size() + users.size());
//            } else {
//                result.put("total", 0);
//            }
//        } else if (qo.getType() == 1) {
//            Page<Strategy> page = strategyService.queryByKeyword(qo);
//            map.put("page", page);
//        } else if (qo.getType() == 2) {
//            Page<Travel> page = travelService.queryByKeyword(qo);
//            map.put("page", page);
//        } else if (qo.getType() == 3) {
//            Page<UserInfo> page = userInfoService.queryByKeyword(qo);
//            map.put("page", page);
//        } else {
//            List<Destination> destinations = destinationService.listByKeyword(qo);
//            result.put("dests", destinations);
//            result.put("strategies", strategies);
//            result.put("travels", travels);
//            result.put("users", users);
//            result.put("total", destinations.size() + strategies.size()
//                    + travels.size() + users.size());
//        }
//        map.put("qo", qo);
//        map.put("result", result);
//        return JsonResult.success(map);
//    }

    @GetMapping("/q")
    public Object searchA(@ModelAttribute("qo")SearchQueryObject qo) throws UnsupportedEncodingException {
        String keyword = URLDecoder.decode(qo.getKeyword(), "utf-8");
        qo.setKeyword(keyword);
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        switch (qo.getType()) {
            case 0:
                return searchDestination(qo, map, result);
            case 1:
                return searchStrategy(qo, map);
            case 2:
                return searchTravel(qo, map);
            case 3:
                return searchUser(qo, map);
            default:
                return search(qo, map, result);
        }
    }

    //查目的地---精确查询
    private Object searchDestination(SearchQueryObject qo, Map map, Map result) {

        //es---先通过destName匹配，然后找到ids集合，再通过ids查mongodb得到数据集合
        //mongodb---先通过destName匹配，得到数据集合
        List<Strategy> strategies = strategyService.listByKeyword(qo);
        List<Travel> travels = travelService.listByKeyword(qo);
        List<UserInfo> users = userInfoService.listByKeyword(qo);
        Destination destination = destinationService.findByKeyword(qo);
        if (destination != null) {
            result.put("strategies", strategies);
            result.put("travels", travels);
            result.put("users", users);
            map.put("dest", destination);
            result.put("total", 1 + strategies.size()
                    + travels.size() + users.size());
        } else {
            result.put("total", 0);
        }
        map.put("qo", qo);
        map.put("result", result);
        return JsonResult.success(map);
    }


    //全文搜索
    //查用户
    private Object searchUser(SearchQueryObject qo, Map map) {
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME
                , UserInfo.class, qo, "info", "city");
        map.put("page", page);
        map.put("qo", qo);
        return JsonResult.success(map);
    }

    //查攻略
    private Object searchStrategy(SearchQueryObject qo, Map map) {
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME
                , Strategy.class, qo, "title", "subTitle", "summary");
        map.put("page", page);
        map.put("qo", qo);
        return JsonResult.success(map);
    }

    //查游记
    private Object searchTravel(SearchQueryObject qo, Map map) {
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME
                , Travel.class, qo, "title", "summary");
        map.put("page", page);
        map.put("qo", qo);
        return JsonResult.success(map);
    }

    //查全部
    private Object search(SearchQueryObject qo, Map map, Map result) {
        Page<Strategy> strategies = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME
                , Strategy.class, qo, "title", "subTitle", "summary");
        Page<Travel> travels = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME
                , Travel.class, qo, "title", "summary");
        Page<UserInfo> users = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME
                , UserInfo.class, qo, "info", "city");
        Page<Destination> destinations = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME
                , Destination.class, qo, "name", "info");
        result.put("dests", destinations.getContent());
        result.put("strategies", strategies.getContent());
        result.put("travels", travels.getContent());
        result.put("users", users.getContent());
        result.put("total", destinations.getTotalElements() + strategies.getTotalElements()
                + travels.getTotalElements() + users.getTotalElements());
        map.put("qo", qo);
        map.put("result", result);
        return JsonResult.success(map);
    }
}
